//app.js
const network = require('/utils/network.js')
const client_api = require('/utils/client_api.js')
App({
  onLaunch: function (options) {
    console.log(options)
    wx.getExtConfigSync()
    wx.getSystemInfo({
      success: (res) => {
        let model = res.model
        console.log(model)
        if (model.includes('iPhone X') || model.includes('iPhone 10')) {
          this.globalData.isPX = 'isPX'
        }
      }
    })
    this.globalData.post = network.post
    this.globalData.get = network.get
    this.globalData.upload = network.upload
    this.globalData.shopId = 1
    this.globalData.serverUrl = client_api.serverUrl
    
    this.globalData.commonDataLoaded = false
    this.globalData.getCommonData = () => {
      return new Promise(resolve => {
        if (this.globalData.commonDataLoaded){
          resolve()
          return
        }
        let getInfo = new Promise(resolve => {
          this.globalData.post({
            url: '/client/getInfo',
            data: {
              shopId: this.globalData.shopId
            },
            success: (data) => {
              this.globalData.clientInfo = data.data
              resolve(data.data)
            }
          })
        })

        let getLevel = new Promise(resolve => {
          this.globalData.post({
            url: '/level/get',
            data: {
              shopId: this.globalData.shopId
            },
            success: (data) => {
              let levelObj = data.data
              levelObj.levelDetail.forEach(val => {
                val.moneyPerPoint = val.moneyPerPoint / 100
                val.discount = val.discount / 10
              })

              if (levelObj.levelDetail[0].name == this.globalData.code) {
                levelObj.levelDetail.shift()
              }

              // this.globalData.levelDetail = levelObj.levelDetail
              this.globalData.levelObj = levelObj
              resolve(data.data)
            }
          })
        })
        
        Promise.all([getLevel, getInfo]).then(data => {
          let levelObj = this.globalData.levelObj
          let clientInfo = this.globalData.clientInfo
          let filterIndex = levelObj.levelDetail.findIndex(val => {
            return val.level == clientInfo.level
          })
          clientInfo.levelIndex = filterIndex
          if (filterIndex !== -1){
            clientInfo.levelName = levelObj.levelDetail[filterIndex].name
          }
          
//           let filterLevel = this.globalData.levelObj.levelDetail.filter(val => {
//             return val.level == this.globalData.clientInfo.level
//           })

//           if (filterLevel.length) {
// this.globalData.clientInfo.levelName = filterLevel[0].name
//           }
          this.globalData.commonDataLoaded = true
          resolve()
        })
      })
    }
    this.globalData.getCommonData()

    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  globalData: {
    userInfo: null,
    code: '96a3be3cf272e017046d1b2674a52bd3',
    isPX: ''
  },
  
})